/**
 * 
 * @description An one Player engine copy
 * @constructor
 * @class
 * @version 0.1a
 */
function SntPlayer(id, swfobject){
	this.id = id;
	this.c = 0;	
	this.registered = false;
	this.player = null;	
	this.eventListeners = {};
	window.getSntHolderInstance().registerPlayer(this.id, this);
}

/**
 * @function
 * @description This function change player status or return it
 * @param {boolean} status It's optional parameter to set status of the player
 * @return boolean
 */
SntPlayer.prototype.register = function(status){
	if(status == null)return this.registered;
	this.registered = status;
	return this.registered;
}

/**
 * @function
 * @description Method for sending messages to holder
 * @see SntEventsHolder#sendMessage
 * @param command It's ID from which command we receive message
 * @param args Array with data
 * @param callback Callback function
 */
SntPlayer.prototype.sendMessage = function(command, args, callback){
	window.getSntHolderInstance().sendMessage(command, this.id, args);
	this.eventListeners[command] = callback;
}


/**
 * @function
 * @description Method for receiving messages from holder
 * @see SntEventsHolder#receiveMessage
 * @param command It's ID from which command we receive message
 * @param args Array with data
 */
SntPlayer.prototype.receiveMessage = function(command, args){
	if(this.eventListeners[command] != null){
		var mNum = this.c++;
		var name = 'f' + mNum;
		this[name] = this.eventListeners[command]; this[name](args);
		this[name] = null;
	}
}


/**
 * @function
 * @description isArray function - Checking whether object is Array or not
 * @param object test Object
 * @returns Boolean
 */
SntPlayer.prototype._isArray = function(o){
	return o && typeof o === "object" && o.constructor === Array;
}
/**
 * @function
 * @description Add new event to event listeners
 * @see SntEventsHolder#addEventListener
 * @param event String or Array Event ID or IDs from Player engine events list
 * @param callback Callback funciton
 */
SntPlayer.prototype.addEventListener = function(eventObject, callback){
	//add just one event if event isn't Array
	var events = eventObject;
	if(!this._isArray(eventObject)){
		events = [eventObject];
	}
	for(var i in events){
		this.eventListeners[events[i]] = callback;
		window.getSntHolderInstance().addEventListener(events[i], this.id);			
	}	
}

/**
 * @function
 * @description Remove an event frome event listener
 * @see SntEventsHolder#removeEventListener
 * @param event String or Array Event ID or IDs from Player engine events list
 * @param callback Callback funciton
 */
SntPlayer.prototype.removeEventListener = function(eventObject){	
	var events = eventObject;
	if(!this.isArray(eventObject)){
		events = [eventObject];
	}
	for(var i in events){
		this.eventListeners[event[i]] = null;	
		window.getSntHolderInstance().removeEventListener(events[i], this.id);
	}
}

/**
 * @function
 * @description Add ready callback to player engine
 * @see SntEventsHolder#addEventListener
 * @param callback Callback funciton
 */
SntPlayer.prototype.ready = function(callback){
	// if player already sent register event
	this.addEventListener(Sonettic.Helper.getConstant('EVENT_REGISTER'), callback);
	if(this.register()){
		this.receiveMessage(Sonettic.Helper.getConstant('EVENT_REGISTER'));
	}
	
}


